package com.coffee.web.controller;

import com.alibaba.fastjson.JSONObject;
import com.coffee.admin.api.platform.result.PlatformBaseDeployResult;
import com.coffee.admin.api.platform.service.PlatformBaseDeployService;
import com.coffee.admin.api.supplier.query.SupplierQuery;
import com.coffee.admin.api.supplier.result.SupplierResult;
import com.coffee.admin.api.supplier.service.SupplierService;
import com.coffee.core.anotation.LoginCheck;
import com.coffee.core.anotation.OperLog;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.result.Result;
import com.coffee.sku.api.inquire.query.InquireSkuContentQuery;
import com.coffee.sku.api.inquire.req.MerchantInquireSkuListReq;
import com.coffee.sku.api.inquire.result.InquireSkuContentResult;
import com.coffee.sku.api.inquire.service.InquireSkuContentService;
import com.coffee.sku.api.sku.service.SkuService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/merchant")
public class InquireSkuController extends BaseController {

    @DubboReference(check = false)
    private SkuService skuService;
    @DubboReference(check = false)
    private InquireSkuContentService inquireSkuContentService;
    @DubboReference(check = false)
    private PlatformBaseDeployService platformBaseDeployService;
    @DubboReference(check = false)
    private SupplierService supplierService;

    @OperLog(operModul = "商户-期货商品列表", operDesc = "期货商品列表")
    @RequestMapping(value = "/inquire/list", method = {RequestMethod.POST})
    public Result<Page<InquireSkuContentResult>> findList(@RequestBody MerchantInquireSkuListReq req, HttpServletRequest request) {
        Result<Page<InquireSkuContentResult>> result = Result.success(null);
        PagerInfo pagerInfo = setDefaultPageInfo(req.getPageSize(), req.getPageIndex());
        InquireSkuContentQuery inquireSkuContentQuery = new InquireSkuContentQuery();
        inquireSkuContentQuery.setIsEnable("Y");
        inquireSkuContentQuery.setIsDisable("N");
        inquireSkuContentQuery.setIsApprove("Y");
        if (req.getOriginPrice() != null) {
            inquireSkuContentQuery.setOriginPrice(req.getOriginPrice() + "");
        }
        if (req.getLevel() != null) {
            inquireSkuContentQuery.setLevel(req.getLevel() + "");
        }
        inquireSkuContentQuery.setSupplierId(req.getSupplierId());
        try {
            result.setData(inquireSkuContentService.getPager(inquireSkuContentQuery, pagerInfo));
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        return result;
    }

    @OperLog(operModul = "供应商-基础数据", operDesc = "基础数据")
    @RequestMapping(value = "/baseDeploy/list", method = {RequestMethod.POST})
    public Result<List<PlatformBaseDeployResult>> baseDeployList(HttpServletRequest request) {
        Result<List<PlatformBaseDeployResult>> result = Result.success(null);
        List<PlatformBaseDeployResult> list = platformBaseDeployService.getList();
        result.setData(list);
        return result;
    }


    @OperLog(operModul = "商户-商品详情", operDesc = "商品详情")
    @LoginCheck
    @RequestMapping(value = "/inquire/sku/detail", method = {RequestMethod.POST})
    public Result<InquireSkuContentResult> skuDetail(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
        Integer id = jsonObject.getInteger("id");
        Result<InquireSkuContentResult> result = Result.success(null);
        try {
            result.setData(inquireSkuContentService.getMerchantDetail(id));
        } catch (BusinessException e) {
            return Result.failed(e.getCode(), e.getMessage());
        }
        return result;
    }


    @OperLog(operModul = "商户-供应商列表", operDesc = "供应商列表")
    @RequestMapping(value = "supplier/list", method = {RequestMethod.POST})
    public Result<List<SupplierResult>> skuDetail(HttpServletRequest request) {
        Result<List<SupplierResult>> result = Result.success(null);
            SupplierQuery supplierQuery=new SupplierQuery();
            supplierQuery.setPid(0);
            supplierQuery.setIsEnable("Y");
            result.setData(supplierService.getList(supplierQuery));
        return result;
    }
}
